﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ScriptManager.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the ScriptManager class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the ScriptManager class.
    /// </summary>
    public static class ScriptManager
    {
        /// <summary>
        /// Initializes a new instance of the ScriptManager class.
        /// </summary>
        /// <param name="scriptId">The identity of the database table's row referencing this script.</param>
        public static BusinessObjects.Script GetScript(int scriptId)
        {
            return Data.Script.Select(scriptId);
        }

        /// <summary>
        /// Gets a dictionary of scripts for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="scriptType">The type of scripts to return (CSS or JS).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced script's name.</returns>
        public static Dictionary<int, string> GetScripts(ScriptType scriptType)
        {
            List<BusinessObjects.Script> scripts = (List<BusinessObjects.Script>)Data.Script.SelectAllWithoutText(scriptType);
            Dictionary<int, string> dicScripts = new Dictionary<int, string>();
            for (int i = 0; i < scripts.Count; i++)
            {
                dicScripts.Add(scripts[i].Id, scripts[i].Name);
            }

            return dicScripts;
        }

        /// <summary>
        /// Gets the titles of pages related to this script.
        /// </summary>
        /// <returns>A readonly list of the titles of the pages referencing this script.</returns>
        public static List<string> GetPages(BusinessObjects.Script script)
        {
            return Data.PageLayout.SelectAllPageNames(script);
        }

        /// <summary>
        /// Inserts the Script object into the database.
        /// </summary>
        /// <returns>The inserted Script with a newly populated Id field.</returns>
        public static BusinessObjects.Script SaveAs(BusinessObjects.Script script)
        {
            return Data.Script.Insert(script);
        }

        /// <summary>
        /// Updates the Script's database fields.
        /// </summary>
        public static void Save(BusinessObjects.Script script)
        {
            Data.Script.Update(script);
        }

        /// <summary>
        /// Deletes the Script's database entry and nullifies and page references to this script.
        /// </summary>
        public static void Delete(BusinessObjects.Script script)
        {
            Data.Script.Delete(script);
        }
    }
}